//
//  BaseViewController.swift
//  Duyiguqin
//
//  Created by 张昭 on 2024/7/29.
//

import UIKit

class BaseViewController: UIViewController {
    
    override func loadView() {
        let v = UIImageView()
        v.image = UIImage(resource: .pageBg)
        v.contentMode = .scaleAspectFill
        v.isUserInteractionEnabled = true
        self.view = v
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationController?.setNavigationBarHidden(true, animated: true)
        
        view.backgroundColor = .white
        view.addSubview(naviHeaderView)
        naviHeaderView.snp.makeConstraints { make in
            make.height.equalTo(view.safeAreaInsets.top + 44)
            make.left.right.top.equalTo(0)
        }
    }
    
    override func viewSafeAreaInsetsDidChange() {
        super.viewSafeAreaInsetsDidChange()
        naviHeaderView.snp.updateConstraints { make in
            make.height.equalTo(view.safeAreaInsets.top + 44)
        }
    }
    
    lazy var naviHeaderView: UIImageView = {
        let v = UIImageView()
        v.image = UIImage(resource: .pageBg)
        v.contentMode = .scaleAspectFit
        v.clipsToBounds = true
        v.isUserInteractionEnabled = true
        v.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.height.equalTo(44)
            make.bottom.equalTo(0)
        }
        v.addSubview(backButton)
        backButton.snp.makeConstraints { make in
            make.size.equalTo(44)
            make.left.bottom.equalTo(0)
        }
        return v
    }()
    
    lazy var titleLabel: UILabel = {
        let v = UILabel()
        v.text = "度一古琴"
        v.textColor = UIColor(0x0F0F0F)
        v.font = UIFont.boldSystemFont(ofSize: 18)
        return v
    }()
    
    private lazy var backButton: UIButton = {
        let v = UIButton(type: .custom)
        v.setImage(UIImage(resource: .naviBack), for: .normal)
        v.addTarget(self, action: #selector(onClickButton(_:)), for: .touchUpInside)
        return v
    }()
}

extension BaseViewController {
    
    @objc func onClickButton(_ sender: UIButton) {
        navigationController?.popViewController(animated: true)
    }
}
